package org.thanos.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.thanos.demo.api.DemoService;
import org.thanos.demo.entity.Demo;

import java.util.Map;
import java.util.WeakHashMap;

@RestController
public class HelloController {

    @Autowired
    private DemoService demoService;

    @GetMapping("/get")
    public Map<String, Object> get(@RequestParam("name") String name) {
        Map<String, Object> result = new WeakHashMap<>();
        result.put("getDemo", demoService.getDemo(name));
        return result;
    }

    @GetMapping("/get/{name}")
    public Map<String, Object> getParams(@PathVariable("name") String name) {
        Map<String, Object> result = new WeakHashMap<>();
        result.put("getDemoParam", demoService.getDemoParam(name));
        return result;
    }

    @PostMapping("/post")
    public Map<String, Object> post(@RequestParam("name") String name) {
        Map<String, Object> result = new WeakHashMap<>();
        result.put("postDemo", demoService.postDemo(name));
        return result;
    }

    @PostMapping("/postBody")
    public Map<String, Object> postBody(@RequestBody Demo demo) {
        Map<String, Object> result = new WeakHashMap<>();
        result.put("requestDemo", demoService.requestDemo(demo));
        return result;
    }


    @GetMapping("/test/{name}")
    public Map<String, Object> test(@PathVariable("name") String name) {
        Map<String, Object> result = new WeakHashMap<>();
        result.put("getDemo", demoService.getDemo(name));
        result.put("getDemoParam", demoService.getDemoParam(name));
        result.put("postDemo", demoService.postDemo(name));
        Demo demo = new Demo();
        demo.setName(name);
        result.put("requestDemo", demoService.requestDemo(demo));
        return result;
    }
}
